import 'dart:convert';
import 'dart:typed_data';

import 'package:car_lrp_plugin_example/scan_car_plate_view.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:car_lrp_plugin/car_lrp_plugin.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  var codeStr = "";
  Uint8List? imageData;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: LayoutBuilder(builder: (context,co){
        return Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () async {
                  // var code = await CarLrpPlugin.openScan(needApply: false);
                  Navigator.of(context).push(MaterialPageRoute(builder: (context){
                    return const ScanCarPlatePage();
                  }));

                },
                child: const Text("自定义view"),
              ),
              Container(height: 10,),
              ElevatedButton(
                onPressed: () async {
                  var code = await CarLrpPlugin.openScan(needApply: false);
                  if(code!=null){
                    setState(() {
                      codeStr = code.code;
                      if(code.frame!=null){
                        imageData = code.frame;
                        print("size:===>${imageData!.length}");
                      }
                    });
                  }
                },
                child: const Text("开始扫描"),
              ),
              Container(height: 10,),
              Text(codeStr),
              imageData==null?Container():
              Image.memory(imageData!)
            ],
          ),
        );
      },),
    );
  }
}
